<template>
  <el-dialog
    width="400px"
    :visible.sync="visible"
    title="修改密码"
    :before-close="close"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" label-width="100px">
      <el-form-item label="旧密码">
        <el-input
          type="password"
          size="mini"
          v-model="form.oldPassword"
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码">
        <el-input
          type="password"
          size="mini"
          v-model="form.newPassword"
        ></el-input>
      </el-form-item>
      <el-form-item label="重复新密码">
        <el-input
          type="password"
          size="mini"
          v-model="form.repeatPassword"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button size="mini" type="primary" @click="onSave">保存</el-button>
        <el-button size="mini" @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import md5 from 'js-md5'
export default {
  name: "EditPasswordDialog",
  data() {
    return {
      department: [],
      form: {
        oldPassword: "",
        newPassword: "",
        repeatPassword: "",
      },
      visible: false,
      isAdd: false, // 是否为添加
    };
  },
  created() {},
  mounted() {},
  methods: {
    open() {
      this.form = {};
      this.visible = true;
    },
    close() {
      this.visible = false;
      this.$emit("close");
    },
    onSave() {

      if (this.form.newPassword != this.form.repeatPassword) {
        this.$message({
          message: "两次密码输入不一致",
          type: "error",
        });
        return;
      }

      let obj = {}
      obj.old_password = md5(this.form.oldPassword)
      obj.password = md5(this.form.newPassword)
      

      this.$put("/user/password", obj).then((res) => {
        if (res.error == 0) {
          this.$message({
            message: "密码修改成功",
            type: "success",
          });
          this.close();
        } else {
          this.$message({
            message: res.msg,
            type: "error",
          });
        }
        this.$emit("closeDialog");
      });
    },
    onCancel() {
      this.close();
    },
  },
};
</script>

<style scoped lang="scss">
.content {
  box-sizing: border-box;
  overflow: hidden;
  .item {
    width: 300px;
    margin: 10px;
    float: left;
    .title {
      height: 100%;
      margin-top: 5px;
      float: left;
    }
    .el-input,
    .el-select,
    .el-input-number {
      width: 200px;
      float: right;
    }
  }
}
.foot {
  overflow: hidden;
  .el-button {
    margin: 10px;
    float: right;
  }
}
</style>


